import Vue from "vue";
import Router from "vue-router";

Vue.use(Router);

/* Layout */
import Layout from "@/layout";
import account from "./account.js";

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRoutes = [
	{
		path: "/login",
		component: () => import("@/views/login/index"),
		hidden: true,
	},

	{
		path: "/404",
		component: () => import("@/views/404"),
		hidden: true,
	},

	{
		path: "/",
		component: Layout,
		redirect: "/home",
		children: [
			{
				path: "home",
				name: "home",
				component: () => import("@/views/home/index"),
				meta: { title: "home", icon: "home" },
			},
		],
	},

	{
		path: "/example",
		component: Layout,
		redirect: "/example/table",
		name: "Example",
		meta: { title: "Example", icon: "el-icon-s-help" },
		children: [
			{
				path: "table",
				name: "Table",
				component: () => import("@/views/table/index"),
				meta: { title: "Table", icon: "table" },
			},
			{
				path: "tree",
				name: "Tree",
				component: () => import("@/views/tree/index"),
				meta: { title: "Tree", icon: "tree" },
			},
		],
	},

	// 404 page must be placed at the end !!!
	{ path: "*", redirect: "/404", hidden: true },
];

let asyncRoutes = [];
asyncRoutes.push(account)

const createRouter = () =>
	new Router({
		mode: 'history',
		scrollBehavior: () => ({ y: 0 }),
		routes: constantRoutes.concat(asyncRoutes),
	});

const router = createRouter();

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
	const newRouter = createRouter();
	router.matcher = newRouter.matcher;
}

export default router;
